import 'dart:convert';

import 'package:demo/model/user.dart';

class Chat {
  User from;
  User to;
  String content;
  String type;

  Chat({
    required this.from,
    required this.to,
    this.content = '',
    this.type = '',
  });

  factory Chat.fromJson(dynamic json) {
    Map<String, dynamic> jsonObject;
    if (json is String) {
      jsonObject = jsonDecode(json);
    } else if (json == null) {
      return Chat(
        from: User(),
        to: User(),
      );
    } else {
      jsonObject = json;
    }
    return Chat(
      from: jsonObject['from'] ?? User(),
      to: jsonObject['to'] ?? User(),
      content: jsonObject['content'] ?? '',
      type: jsonObject['type'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'from': from,
      'to': to,
      'content': content,
      'type': type,
    };
  }

  @override
  String toString() {
    return jsonEncode(this);
  }
}
